
import { CONTROL_TYPES } from '@/common/const-wb'
import { EventBus } from '@/store/index'
import { getPxOVwByValue } from '@/store/utils'

export const vote = {
  created(){
    const data_ = {
      voteStr: '',
      voteStr_: '',
      votePositions: []
    };
    for(let key in data_) {
      this[key] = data_[key];
    }
    if(this.commonAttr.votePosition){
      this.votePositions = this.commonAttr.votePosition.split(',');
    }
    this.voteStr_ = this.votePositions[9];
    EventBus.$off("refreshDynamicData", this.refreshItemText);
    EventBus.$on("refreshDynamicData", this.refreshItemText);
    if(this.isVoteStr || this.voteStr_ && this.voteStr_.indexOf("{{num}}") != -1){
      let text = this.getVariableItem.push_text || this.getVariableItem.text;
      this.setVoteStr(text, this.getVariableItem.num ? this.getVariableItem.num : 0);
    }else{
      this.setVoteStr('', this.getVariableItem.num ? this.getVariableItem.num : 0);
    }
  },
  computed: {
    isVoteStr(){
      let text = this.getVariableItem.push_text || this.getVariableItem.text;
      return text && text.indexOf("{{num}}") != -1 ? true : false;
    },
    rankUrl() {
      let index = this.index;
      let itemRanks = this.commonAttr.itemRanks && this.commonAttr.itemRanks.split(",");
      let len = itemRanks.length - 1;
      let url = "";
      if (index <= len) {
        url = itemRanks[index];
      }
      return url;
    },
    medalUrl(){
      let index = this.index;
      let itemMedals = this.commonAttr.itemMedals && this.commonAttr.itemMedals.split(",");
      let len = itemMedals.length - 1;
      let url = "";
      if (index <= len) {
        url = itemMedals[index];
      }
      return url;
    },
    num(){
      let voteItem = this.wbListOrLists[this.index];
      return voteItem && voteItem.num ? parseInt(voteItem.num) : 0;
    },
    medalPosition(){
      //排行排序数
      return this.positionStyle(this.commonAttr.medalPosition);
    },
    crownPosition(){
      //皇冠
      return this.positionStyle(this.commonAttr.crownPosition);
    },
    votePosition(){
      //票数 340,70,140,36,24,#fff,normal,#ff8094,20,false
      // {left,top,width,height,fontSize,color,fontStyle,backgroundColor,borderRadius,ticketStr,textAlign}
      let style = this.positionStyle(this.commonAttr.votePosition);
      const positionAttr = this.votePositions;

      if(positionAttr[10]){
        style.textAlign = positionAttr[10];
      }
      return style
    },
    getImgOrHeadUrl(){
      //用户头像，博文中的图片
      return this.getControlItem.frontPicUrl || this.getControlItem.pic_url || this.getControlItem.user.avatar_large;
    },
    getUserName(){
      if(this.getItemControls[CONTROL_TYPES.wb_user]){
        return this.getItemControls[CONTROL_TYPES.wb_user].data.namePrefix + this.getControlItem.name;
      }else{
        return this.getControlItem.name;
      }
    }
  },
  methods: {
    positionStyle(str){
      // {left,top,width,height,fontSize,color,fontStyle,backgroundColor,borderRadius,ticketStr}
      const positionAttr = str.split(',');
      let styles_ = {};
      if(positionAttr.length > 0){
        styles_ = {
          position: 'absolute',
          left: getPxOVwByValue(positionAttr[0]) ,
          top: getPxOVwByValue(positionAttr[1]) ,
        }
        if(positionAttr[2]){
          styles_.width = getPxOVwByValue(positionAttr[2]);
          styles_.height = getPxOVwByValue(positionAttr[3]);
        }
        if(positionAttr[4]){
          styles_.fontSize = getPxOVwByValue(positionAttr[4]);
          if(!this.voteStr_){
            styles_.color = positionAttr[5];
          }
          styles_.fontStyle = positionAttr[6];
        }
        if(positionAttr[7]){
          styles_.backgroundColor = positionAttr[7];
          styles_.borderRadius = getPxOVwByValue(positionAttr[8]);
        }
      }
      return styles_
    },
    refreshItemText(){
      let voteItem = this.wbListOrLists[this.index];
      let text = voteItem.push_text || voteItem.text;
      if(voteItem.num === undefined){
        voteItem.num = 0;
      }
      this.setVoteStr(text, voteItem.num);
    },
    setVoteStr(text, num){
      let voteStr_ = this.voteStr_;
      if(voteStr_){
        text = voteStr_
        if(this.votePositions[5]){
          this.voteStr = text.replace("{{num}}", '<span style="color:' + this.votePositions[5] + '">' +num + '</span>');
        }else{
          this.voteStr = text.replace("{{num}}",num);
        }
      }else{
        this.voteStr = num + '票';
      }
      // {left,top,width,height,fontSize,color,fontStyle,backgroundColor,borderRadius,ticketStr,textAlign}
    }
  },
  beforeUnmount(){
    EventBus.$on("refreshDynamicData", this.refreshItemText);
  }
}


